package com.yitao.cms.service.pay;

import com.querydsl.core.types.Order;
import com.querydsl.core.types.OrderSpecifier;
import com.skipper.base.service.GenericEntityService;
import com.yitao.client.constant.PayEnum;
import com.yitao.client.dto.pay.PayStreamDto;
import com.yitao.client.model.pay.PayStreamModel;
import com.yitao.client.model.pay.QPayStreamModel;
import com.yitao.cms.dto.order.OrderStreamDto;
import com.yitao.cms.mapper.OrderMapper;
import com.yitao.cms.vo.order.OrderStreamVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.EntityManager;
import java.util.List;

@Service
public class CmsPayStreamService extends GenericEntityService<PayStreamModel, PayStreamDto, QPayStreamModel> {

    public CmsPayStreamService(EntityManager entityManager) {
        super(entityManager);
    }

    @Autowired
    private OrderMapper orderMapper;

    /**
     * 根据订单ID查询订单支付详情
     *
     * @param orderId 订单ID
     * @return
     */
    public PayStreamModel getPayStreamInfoByOrderId(Long orderId) {
        PayStreamModel payStreamModel = null;
        List<PayStreamModel> payStreamModelList = queryFactory.select(Q).from(Q).where(Q.orderId.eq(orderId).
                /*and(Q.payType.eq(PayEnum.PAY_TYPE_ENUM.USER_PAY.getCode())).*/
                        and(Q.payStatus.eq(PayEnum.PayStatus.PAID.getCode()))).orderBy(new OrderSpecifier<>(Order.DESC, Q.createTime)).fetch();
        if (payStreamModelList != null && payStreamModelList.size() != 0) {
            payStreamModel = payStreamModelList.get(0);
        }
        return payStreamModel;
    }

    /**
     * 根据订单ID查询订单退款状态
     *
     * @param orderId 订单ID
     * @return
     */
    public PayStreamModel getPayStreamInfoRefundByOrderId(Long orderId) {
        PayStreamModel payStreamModel = null;
        List<PayStreamModel> payStreamModelList = queryFactory.select(Q).from(Q).where(Q.orderId.eq(orderId).
                /*and(Q.payType.eq(PayEnum.PAY_TYPE_ENUM.USER_PAY.getCode())).*/
                        and(Q.payStatus.eq(PayEnum.PayStatus.REFUNDED.getCode()))).orderBy(new OrderSpecifier<>(Order.DESC, Q.createTime)).fetch();
        if (payStreamModelList != null && payStreamModelList.size() != 0) {
            payStreamModel = payStreamModelList.get(0);
        }
        return payStreamModel;
    }

    public List<OrderStreamDto> getPayStreamList(OrderStreamVo orderStreamVo) {
        return orderMapper.getOrderStreamList(orderStreamVo);
    }


    @Transactional
    public void save(PayStreamModel payStreamModel) {
        repository.save(payStreamModel);
    }

    public List<PayStreamModel> getPayStreamInfoByPayClientTypeList(List<Integer> payClientTypeList, Long shopId) {
      return  queryFactory.select(Q).from(Q).where(Q.shopId.eq(shopId).and(Q.payClientType.in(payClientTypeList)).and(Q.payType.eq(PayEnum.PAY_TYPE_ENUM.USER_PAY.getCode()))).fetch();
    }
}
